﻿using MediatR;
using S.PurchaseAndSales.Api.Application.Command.SheepSaleCommand;
using S.PurchaseAndSales.Domain.SqlServer;
using S.PurchaseAndSales.Infrastructure.Interface.SalesForm;

namespace S.PurchaseAndSales.Api.Application.CommandHandlers.SheepSaleHandler
{
    public class DeleteSheepSaleHandler : IRequestHandler<DeleteSheepSaleCommand, bool>
    {

        private readonly ISheepSaleRepository sheepSale;

        private readonly SqlDbContext db;

        public DeleteSheepSaleHandler(ISheepSaleRepository sheepSale,SqlDbContext db)
        {
            this.sheepSale = sheepSale;
            this.db = db;
        }


        public async Task<bool> Handle(DeleteSheepSaleCommand request, CancellationToken cancellationToken)
        {
            var ids = request.ids.Split(",");
            var list = db.SheepSaleModels.Where(x => ids.Contains(x.SheepSaleId.ToString())).ToList();
            db.SheepSaleModels.RemoveRange(list);
            return await db.SaveChangesAsync() > 0;
        }
    }
}
